home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / pm.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  9.2 KB  |  367 lines

  1. /*
  2.  *    pm.trm  --- inboard terminal driver for Presentation Manager
  3.  *            --- after X-11 driver, by R.W.Fearick 31/1/92.
  4.  *    v1.1 11/8/92 -- speed things up        
  5.  */
  6.  
  7.  
  8.  
  9. #ifndef GOT_DRIVER_H
  10. #include "driver.h"
  11. #endif
  12.  
  13. #ifdef TERM_REGISTER
  14. register_term(pm)
  15. #endif
  16.  
  17. #ifdef TERM_PROTO
  18. TERM_PUBLIC void PM_init __P((void));
  19. TERM_PUBLIC void PM_reset __P((void));
  20. TERM_PUBLIC void PM_text __P((void));
  21. TERM_PUBLIC void PM_graphics __P((void));
  22. TERM_PUBLIC void PM_linetype __P((int lt));
  23. TERM_PUBLIC void PM_move __P((unsigned int x,unsigned int y));
  24. TERM_PUBLIC void PM_vector __P((unsigned int x,unsigned int y));
  25. TERM_PUBLIC int  PM_text_angle __P((int ta));
  26. TERM_PUBLIC void PM_put_text __P((unsigned int x,unsigned int y,char *str));
  27. TERM_PUBLIC int  PM_justify_text __P((enum JUSTIFY mode));
  28. TERM_PUBLIC void PM_point __P((unsigned int x, unsigned int y, int number));
  29. TERM_PUBLIC void PM_suspend __P((void));
  30. TERM_PUBLIC void PM_resume __P((void));
  31.  
  32. /* define PM world coordinate limits */
  33. #define PM_XMAX 4096
  34. #define PM_YMAX 4096
  35.  
  36. /* approximations for typical font/screen sizes */
  37.  
  38. #define PM_VCHAR (PM_YMAX/30) 
  39. #define PM_HCHAR (PM_XMAX/80) 
  40. #define PM_VTIC (PM_YMAX/100)
  41. #define PM_HTIC (PM_XMAX/150)
  42. #endif
  43.  
  44. #ifdef TERM_BODY
  45.  
  46. #include <stdio.h>
  47. #include <process.h>
  48. #include <io.h>
  49.  
  50.  
  51. /* 
  52.    include all stuff from os2.h as GNUPLOT uses INT as an enum and
  53.    this clashes with #defines in os2.h 
  54. */
  55.  
  56. typedef unsigned short USHORT;
  57. typedef USHORT *PUSHORT;
  58. typedef void *PVOID ;
  59. typedef char *PCHAR ;
  60.  
  61. typedef long LONG;
  62. typedef LONG *PLONG;
  63.  
  64. typedef unsigned long ULONG;
  65. typedef ULONG *PULONG;
  66. typedef struct
  67. {
  68.   ULONG  tib2_ultid;
  69.   ULONG  tib2_ulpri;
  70.   ULONG  tib2_version;
  71.   USHORT tib2_usMCCount;
  72.   USHORT tib2_fMCForceFlag;
  73. } TIB2;
  74. typedef TIB2 *PTIB2;
  75.  
  76. typedef struct
  77. {
  78.   PVOID tib_pexchain;
  79.   PVOID tib_pstack;
  80.   PVOID tib_pstacklimit;
  81.   PTIB2 tib_ptib2;
  82.   ULONG tib_version;
  83.   ULONG tib_ordinal;
  84. } TIB;
  85. typedef TIB *PTIB;
  86.  
  87. typedef struct
  88. {
  89.   ULONG pib_ulpid;
  90.   ULONG pib_ulppid;
  91.   ULONG pib_hmte;
  92.   PCHAR pib_pchcmd;
  93.   PCHAR pib_pchenv;
  94.   ULONG pib_flstatus;
  95.   ULONG pib_ultype;
  96. } PIB;
  97. typedef PIB *PPIB;
  98. typedef ULONG HEV;
  99. typedef HEV *PHEV;
  100.  
  101. ULONG DosCreateEventSem (const char *, PHEV, ULONG, ULONG);
  102. ULONG DosWaitEventSem (HEV, ULONG);
  103. ULONG DosGetInfoBlocks (PTIB *, PPIB *);
  104. ULONG DosSearchPath( ULONG, char*, char*, char*, ULONG ) ;
  105.  
  106.  
  107. /* graphics commands */
  108. #define SET_GRAPHICS    'G'
  109. #define SET_TEXT        'E'
  110. #define SET_LINE        'L'
  111. #define SET_ANGLE       'A'
  112. #define SET_JUSTIFY     'J'
  113. #define SET_POINTMODE   'D'
  114. #define GR_SUSPEND      'E' /*'s'*/
  115. #define GR_RESUME       'r'
  116. #define GR_MOVE         'M'
  117. #define GR_DRAW         'V'
  118. #define GR_RESET        'R'
  119. #define GR_TEXT         'T'
  120. #define GR_PAUSE        'P'
  121. #define GR_HELP         'H'
  122. #define PM_nopts 1
  123.  
  124. static char PM_path[256] = "" ;  /* path for pm program */
  125. static int  PM_mode      = 0 ;   /* track mode to avoid redraw after hitting break */
  126. static     HEV hev ;
  127.  
  128. static char PM_opts[PM_nopts][20] = {
  129.    " "
  130.    };
  131. static int PM_optarg[PM_nopts] = { 
  132.    0
  133.    };
  134.  
  135. FILE *fopen();
  136. static FILE *PM_pipe=NULL;
  137. #if 0
  138. static char PM_command[1024]= "gnuplot_PM -name gnuplot";
  139. #endif
  140.  
  141. int  PM_pause __P((char *str));
  142. void PM_setup( void ) ;
  143.  
  144.  
  145. TERM_PUBLIC void PM_init() 
  146.     { 
  147.     static char buffer[1024] ;
  148.     int pid ;
  149.     int rc ;
  150.     PPIB pib ;
  151.     PTIB tib ;
  152.     char semname[32] ;
  153.     char pipename[32] ;
  154.     char tempname[32] ;
  155.     if( PM_pipe == NULL ) {
  156.         strcpy( tempname, "gpXXXXXX" ) ;
  157.         if( mktemp( tempname ) == NULL ) {
  158.             fprintf( stderr, "Temp name failure !\n" ) ;
  159.             abort() ;   
  160.             }
  161.         strcpy( semname, "\\sem32\\" ) ;
  162.         strcpy( pipename, "\\pipe\\" ) ;
  163.         strcat( semname, tempname ) ;
  164.         strcat( pipename, tempname ) ;
  165.         strcat( PM_path, "\\gnupmdrv.exe" ) ;
  166.         rc = access( PM_path, 0 ) ;
  167.             /* find exe file */ 
  168.   
  169.         if( rc != 0 ) 
  170.             rc = DosSearchPath( 0x0002, /* search GNUPLOT environment */
  171.                                 "GNUPLOT",
  172.                                 "gnupmdrv.exe",
  173.                                 PM_path,
  174.                                 256 ) ; 
  175.  
  176.         if( rc != 0 ) 
  177.             rc = DosSearchPath( 0x0003,  /* then try current directory & path */
  178.                                 "PATH",
  179.                                 "gnupmdrv.exe",
  180.                                 PM_path,
  181.                                 256 ) ; 
  182.         if( rc != 0 ) {
  183.             fprintf( stderr, "Can't find gnupmdrv.exe !\n" ) ;
  184.             abort() ;   
  185.             }
  186.                             
  187.         rc = DosCreateEventSem( semname, &hev, 1, 0 ) ;
  188.         if( rc != 0 ) {
  189.             fprintf( stderr, "Can't create semaphore !\n" ) ;
  190.             abort() ;   
  191.             }
  192.         pid=spawnl( P_SESSION|P_DEFAULT, PM_path, "GnuplotPM", tempname, NULL ) ;
  193.       /*  pid=spawnl( P_SESSION|P_DEFAULT, "ipmd", "GnuplotPM", PM_path, tempname, NULL ) ;*/
  194.         if( rc == -1 ) {
  195.             fprintf( stderr, "Can't spawn gnupmdrv.exe !\n" ) ;
  196.             abort() ;   
  197.             }
  198.  
  199.         DosGetInfoBlocks( &tib, &pib ) ;
  200.         DosWaitEventSem( hev, 10000 ) ;        
  201.         PM_pipe = fopen( pipename, "r+b" ) ; 
  202.         if( PM_pipe == NULL ) {
  203.             fprintf( stderr, "Can't open pipe to gnupmdrv.exe !\n" ) ;
  204.             abort() ;   
  205.             }
  206.         setvbuf( PM_pipe, buffer, _IOFBF, 1024 ) ;
  207.         pid = pib->pib_ulpid ;
  208.         fwrite( &pid, 1, 4, PM_pipe ) ;
  209.         fflush( PM_pipe ) ;
  210.         }
  211.     }
  212.  
  213. TERM_PUBLIC void PM_reset() {
  214.         putc( GR_RESET, PM_pipe); 
  215.         fflush(PM_pipe);
  216.         }
  217.  
  218. TERM_PUBLIC void PM_suspend() {
  219.         putc( GR_SUSPEND, PM_pipe); 
  220.         fflush(PM_pipe);
  221.         }
  222.  
  223. TERM_PUBLIC void PM_resume() {
  224.         putc( GR_RESUME, PM_pipe); 
  225.         fflush(PM_pipe);
  226.         }
  227.  
  228. TERM_PUBLIC void PM_text() 
  229.     {
  230.     fflush(PM_pipe);
  231.     if( PM_mode != SET_TEXT ) { 
  232.         putc( SET_TEXT, PM_pipe); 
  233.         fflush(PM_pipe);
  234.         }
  235.     PM_mode = SET_TEXT ;
  236.     }
  237.  
  238. TERM_PUBLIC void PM_graphics() 
  239.     { 
  240.     putc( SET_GRAPHICS, PM_pipe); 
  241.     PM_mode = SET_GRAPHICS ;
  242.     }
  243.  
  244. TERM_PUBLIC void PM_move(unsigned int x, unsigned int y) 
  245.     { 
  246.     putc( GR_MOVE, PM_pipe ) ;
  247.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  248.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  249.     }
  250.  
  251. TERM_PUBLIC void PM_vector(unsigned int x, unsigned int y)
  252.     { 
  253.     putc( GR_DRAW, PM_pipe ) ;
  254.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  255.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  256.     }
  257.  
  258. TERM_PUBLIC void PM_linetype(int lt)
  259.     { 
  260.     putc( SET_LINE, PM_pipe ) ;
  261.     fwrite( <, sizeof(int), 1, PM_pipe ) ;
  262.     }
  263.  
  264. TERM_PUBLIC int PM_text_angle( int ta)
  265.     { 
  266.     putc( SET_ANGLE, PM_pipe ) ;
  267.     fwrite( &ta, sizeof(int), 1, PM_pipe ) ;
  268.     return(TRUE) ; 
  269.     }
  270.  
  271. TERM_PUBLIC void PM_put_text(unsigned int x, unsigned int y, char *str) 
  272.     {
  273.     int len ;
  274.     putc( GR_TEXT, PM_pipe ) ;
  275.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  276.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  277.     len = strlen( str ) + 1 ;
  278.     fwrite( &len, sizeof(int), 1, PM_pipe ) ;
  279.     fwrite( str, 1, len, PM_pipe ) ;
  280.     for( len=sizeof(int)-len%sizeof(int); len > 0 ; len-- )  /* pad rest of int with zeros */
  281.         putc( '\0', PM_pipe ) ;
  282.     }
  283.  
  284. TERM_PUBLIC int PM_justify_text( enum JUSTIFY mode ) 
  285.     {
  286.     putc( SET_JUSTIFY, PM_pipe ) ;
  287.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  288.     return(TRUE);
  289.     }
  290.  
  291. TERM_PUBLIC void PM_point( unsigned int x, unsigned int y, int number )
  292. /*
  293. ** tell the driver we are plotting a point so it can decide whether to
  294. ** use colour or not
  295. */
  296.     {
  297.     int mode ;
  298.     mode=1 ;
  299.     putc( SET_POINTMODE, PM_pipe ) ;
  300.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  301.     do_point( x, y, number ) ;
  302.     mode = 0 ;
  303.     putc( SET_POINTMODE, PM_pipe ) ;
  304.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  305.     }
  306.  
  307. void PM_setup( void )
  308. /*
  309. ** Initial terminal setup
  310. */
  311.     {
  312.     if( PM_path[0]=='\0' ) _getcwd2( PM_path, 256 ) ;
  313.     }
  314.  
  315. int PM_pause( char *str )
  316. /*
  317. ** pause - using message box on PM screen
  318. */
  319.     {
  320.     int len, cbR, rc ;
  321.     unsigned long ul ;
  322.     char buf[256] ;
  323.     char *bp ;
  324.  
  325.     if( PM_pipe == NULL ) return 2 ;
  326.     bp=buf ;
  327.     putc( GR_PAUSE, PM_pipe ) ;
  328.     len = strlen( str ) + 1 ;
  329.     fwrite( &len, sizeof(int), 1, PM_pipe ) ;
  330.     fwrite( str, 1, len, PM_pipe ) ;
  331.     for( rc=sizeof(int)-len%sizeof(int); rc > 0 ; rc-- )  /* pad rest of int with zeros */
  332.         putc( '\0', PM_pipe ) ;
  333.     fflush(PM_pipe ) ;
  334.     rc=DosRead( fileno(PM_pipe), &len, sizeof(int), &cbR ) ;
  335.     return len ;
  336.     }
  337.  
  338. #endif
  339.  
  340. #ifdef TERM_TABLE
  341. TERM_TABLE_START(pm_driver)
  342.     "pm", "OS/2 Presentation Manager", 
  343.            PM_XMAX, PM_YMAX, PM_VCHAR, PM_HCHAR,
  344.            PM_VTIC, PM_HTIC, options_null, PM_init, PM_reset,
  345.            PM_text, null_scale, PM_graphics, PM_move, PM_vector,
  346.            PM_linetype, PM_put_text, PM_text_angle,
  347.            PM_justify_text, PM_point, do_arrow, set_font_null,
  348.            null_set_pointsize, 0, PM_suspend, PM_resume
  349. TERM_TABLE_END(pm_driver)
  350.  
  351. #undef LAST_TERM
  352. #define LAST_TERM pm_driver
  353. #endif
  354.  
  355. /*
  356.  * NAME: pm
  357.  *
  358.  * OPTIONS: none 
  359.  *
  360.  * SUPPORTS: OS/2 Presentation Manager
  361.  *
  362.  * Further Info: none
  363.  *
  364.  * NOTE: Roger Fearick (the author of this file) and Stefan A. Deutscher
  365.  *     are both quite often seen at c.g.a.g and should know a lot
  366.  *     more about this terminal.
  367.  */